<?php


namespace app\admin\controller;


class Jpush extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $param = input();
        $keywords = $param['keywords'];
        $param['page'] = intval($param['page']) < 1 ? 1 : $param['page'];
        $param['limit'] = intval($param['limit']) < 1 ? $this->_pagesize : $param['limit'];
        $where = [];
        if (empty($param['setType'])) {
            if (empty($param['start'])) {
                $param['start'] = strtotime('2020-10-20');
            } else {
                $param['start'] = strtotime(urldecode(urldecode($param['start'])));
            }

            if (empty($param['end'])) {
                $param['end'] = strtotime('tomorrow') - 1;
            } else {
                $param['end'] = strtotime(urldecode(urldecode($param['end'])));
            }
        }
        else {
            switch ($param['setType']) {
                case 'today':
                    $param['start'] = strtotime('today');
                    $param['end'] = strtotime('tomorrow') - 1;
                    break;
                case 'yesterday':
                    $param['start'] = strtotime('yesterday');
                    $param['end'] = strtotime('today');
                    break;
                case 'week':
                    $param['start'] = strtotime('-1 week', strtotime('today'));
                    $param['end'] = time();
                    break;
                default:
                    unset($param['setType']);
                    break;
            }
        }

        if ($param['end'] <= $param['start']) {
            $this->error('结束时间不能早于开始时间');
        }
        $where['time_added'] = ['between', [$param['start'], $param['end']]];

        if (!empty($param['app_id'])) {
            $where['app_id'] = $param['app_id'];
        }
        if(!empty($param['keywords'])){
            $where['device_id|device_name|vod_name']=['like','%'.$param['keywords'].'%'];
        }

        $where['status']=2;

        $order = 'time_added desc';
        $page = $param['page'];
        $limit = $param['limit'];
        $start = 0;
        $limit_str = ($limit * ($page - 1) + $start) . "," . $limit;


        //得到ios数据
        $iOSNum = \db('push_job')->where($where)->where('system_type', 'ios')->count();
        $iOSUniqueNum = \db('push_job')->where($where)->where('system_type', 'ios')->group('app_id,device_id')->count();
        $iOSClickNum = \db('push_job')->where($where)->where('click_status',1)->where('system_type', 'ios')->count();
        $iOSClickUniqueNum = \db('push_job')->where($where)->where('click_status',1)->where('system_type', 'ios')->group('app_id,device_id')->count();
        $androidNum = \db('push_job')->where($where)->where('system_type', 'android')->count();
        $androidUniqueNum = \db('push_job')->where($where)->where('system_type', 'android')->group('app_id,device_id')->count();
        $androidClickNum = \db('push_job')->where($where)->where('click_status',1)->where('system_type', 'android')->count();
        $androidClickUniqueNum = \db('push_job')->where($where)->where('click_status',1)->group('app_id,device_id')->where('system_type', 'android')->count();

        $applists = \db('banben')->column('app_id,appname as app_name');


        $total = \db('push_job')->where($where)->count();
        $list = \db('push_job')->where($where)->orderRaw($order)->limit($limit_str)->select();
//        dump(\db('user_history')->getLastSql());
//exit;
        $this->assign('list', $list);
        $this->assign('total', $total);
        $this->assign('page', $page);
        $this->assign('limit', $limit);
        $this->assign('iOSNum', $iOSNum);
        $this->assign('iOSUniqueNum', $iOSUniqueNum);
        $this->assign('iOSClickNum', $iOSClickNum);
        $this->assign('iOSClickUniqueNum', $iOSClickUniqueNum);
        $this->assign('androidNum', $androidNum);
        $this->assign('androidUniqueNum', $androidUniqueNum);
        $this->assign('androidClickNum', $androidClickNum);
        $this->assign('androidClickUniqueNum', $androidClickUniqueNum);
        $this->assign('applists', $applists);

        $param['page'] = '{page}';
        $param['limit'] = '{limit}';
        $param['start'] = date('Y-m-d H:i:s', $param['start']);
        $param['end'] = date('Y-m-d H:i:s', $param['end']);
//        dump($param);
        $this->assign('param', $param);
        $this->assign('title', '极光推送');
        return $this->fetch('admin@jpush/index');
    }

}